﻿using System;
using TaobaoAPIDAO.App_Code;
using Top.Api.Domain;
using Top.Api.Response;
using Top.Api.Request;

namespace TaobaoAPIDAO.TopApiDao
{
    public class TradeApiDao : BaseTopApiDao
    {
        #region public TradesSoldGetResponse TaobaoTradesBoughtGet(string,string,string,string,string,DateTime,DateTime) 搜索当前会话用户作为买家达成的交易记录
        /// <summary>
        /// 搜索当前会话用户作为买家达成的交易记录
        /// </summary>
        /// <param name="sessionKey"></param>
        /// <param name="Status"></param>
        /// <param name="SellerNick"></param>
        /// <param name="Type"></param>
        /// <param name="RateStatus"></param>
        /// <param name="StartCreated"></param>
        /// <param name="EndCreated"></param>
        /// <returns></returns>
        public TradesBoughtGetResponse TaobaoTradesBoughtGet(string sessionKey, string Status, string SellerNick,
            string Type, string RateStatus, DateTime StartCreated, DateTime EndCreated)
        {
            //实例化具体API对应的Request类 
            TradesBoughtGetRequest req = new TradesBoughtGetRequest();
            req.Fields = TradeApiFiled.TradeApiField;
            req.StartCreated = StartCreated;
            req.EndCreated = EndCreated;
            req.Status = Status;
            req.SellerNick = SellerNick;
            req.Type = Type;
            req.RateStatus = RateStatus;
            TradesBoughtGetResponse response = myclient.Execute(req, sessionKey);
            return response;
        }
        #endregion

        #region public TradesSoldGetResponse TaobaoTradesSoldGet(string,string,string,string,string,DateTime,DateTime) 搜索当前会话用户作为卖家达成的交易记录
        /// <summary>
        /// 搜索当前会话用户作为卖家达成的交易记录
        /// </summary>
        /// <param name="TopSessionKey"></param>
        /// <param name="StartCreated"></param>
        /// <param name="EndCreated"></param>
        /// <param name="Status"></param>
        /// <param name="BuyerNick"></param>
        /// <param name="Type"></param>
        /// <param name="RateStatus"></param>
        /// <returns></returns>
        public TradesSoldGetResponse TaobaoTradesSoldGet(string TopSessionKey,DateTime StartCreated, DateTime EndCreated, string Status, 
            string BuyerNick, string Type, string RateStatus)
        {
            //实例化具体API对应的Request类 
            TradesSoldGetRequest req = new TradesSoldGetRequest();

            //需要查询的字段信息
            req.Fields = TradeApiFiled.TradeApiField;
            //查询开始的时间
            req.StartCreated = StartCreated;
            //查询截止时间
            req.EndCreated = EndCreated;

            //交易状态
            if (!String.IsNullOrEmpty(Status))
            {
                req.Status = Status;
            }

            //买家Nick
            if (!String.IsNullOrEmpty(BuyerNick))
            {
                req.BuyerNick = BuyerNick;
            }

            //交易类型列表
            if (!String.IsNullOrEmpty(Type))
            {
                req.Type = Type;
            }

            //评价状态
            if (!String.IsNullOrEmpty(RateStatus))
            {
                req.RateStatus = RateStatus;
            }

            TradesSoldGetResponse response = myclient.Execute(req, TopSessionKey);
            return response;
        }
        #endregion

        #region public TradesSoldGetResponse TaobaoTradesSoldGet(string,string,string,string,string,DateTime,DateTime) 搜索当前会话用户作为卖家达成的交易记录
        /// <summary>
        /// 搜索当前会话用户作为卖家达成的交易记录
        /// </summary>
        /// <param name="TopSessionKey"></param>
        /// <param name="StartCreated"></param>
        /// <param name="EndCreated"></param>
        /// <param name="Status"></param>
        /// <param name="BuyerNick"></param>
        /// <param name="Type"></param>
        /// <param name="RateStatus"></param>
        /// <returns></returns>
        public TradesSoldIncrementGetResponse TaobaoTradesIncrmentSoldGet(string TopSessionKey, DateTime StartCreated, DateTime EndCreated, string Status,
            string BuyerNick, string Type, string RateStatus)
        {
            TradesSoldIncrementGetRequest req = new TradesSoldIncrementGetRequest();
            req.Fields = TradeApiFiled.TradeApiField;
            req.StartModified = StartCreated;
            req.EndModified = EndCreated;

            //交易状态
            if (!String.IsNullOrEmpty(Status))
            {
                req.Status = Status;
            }

            //交易类型列表
            if (!String.IsNullOrEmpty(Type))
            {
                req.Type = Type;
            }
            TradesSoldIncrementGetResponse response = myclient.Execute(req, TopSessionKey);
            return response;
        }
        #endregion

        #region public TradesSoldGetResponse GetTradesSoldInfoWithDate() 获取用户的交易数据(指定时间段)。
        /// <summary>
        /// 获取用户的交易数据(指定时间段,买家为可选参数)。
        /// </summary>
        /// <param name="Nick">买家Nick</param>
        /// <param name="TopSessionKey">SessionKey</param>
        /// <param name="startDate">开始数据</param>
        /// <param name="EndDate">截至时间</param>
        /// <returns></returns>
        public Top.Api.Domain.Trade GetTradesSoldInfoWithDate(string Nick, string TopSessionKey,DateTime startDate,DateTime EndDate)
        {
            //实例化具体API对应的Request类 
            TradesSoldGetRequest req = new TradesSoldGetRequest();
            //需要查询的字段信息
            req.Fields = TradeApiFiled.TradeApiField;
            req.StartCreated = startDate;
            req.EndCreated = EndDate;
            if (!string.IsNullOrEmpty(Nick))
            {
                req.BuyerNick = Nick;
            }

            TradesSoldGetResponse response = myclient.Execute(req, TopSessionKey);

            Trade trade = XmlHelper.LoadObjFromXMLForTrade(response.Body);
            return trade;
        }
        #endregion

        #region public void TaobaoTradeGet(string sessionKey,long Tid) 获取单笔交易的部分信息(性能高)
        /// <summary>
        /// 获取单笔交易的部分信息(性能高)
        /// </summary>
        /// <param name="sessionKey"></param>
        /// <param name="Tid"></param>
        public TradeGetResponse TaobaoTradeGet(string sessionKey, long Tid)
        {
            TradeGetRequest req = new TradeGetRequest();
            req.Fields = TradeApiFiled.TradeApiField;
            req.Tid = Tid;
            TradeGetResponse response = myclient.Execute(req, sessionKey);
            return response;
        }

        #endregion

    }
}
